package practice_2025_10.practice_10_9;

class Solution {
    /**
     * 最长回文串
     * 中心扩展
     * @param s
     * @return
     */
    public String longestPalindrome(String s) {
        int begin = 0, count = 0;
        int len = s.length();
        int left = 0, right = 0;
        for(int i = 0; i < len; i++) {
            // 以 i 位置为中心扩展
            left = i; right = i;
            while(left >= 0 && right < len && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
            }
            if (right - left - 1 > count) {
                begin = left + 1;
                count = right - left - 1;
            }
            // 以 i & i + 1位置扩展
            left = i; right = i + 1;
            while(left >= 0 && right < len && s.charAt(left) == s.charAt(right)) {
                left--;
                right++;
            }
            if (right - left - 1 > count) {
                begin = left + 1;
                count = right - left - 1;
            }
        }
        return s.substring(begin, begin + count);
    }

    /**
     * 无重复字符的最长子串
     * 双指针
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {
        int[] hash = new int[128];
        int maxCount = 0;
        for(int left = 0, right = 0; right < s.length(); right++) {
            int in = s.charAt(right);
            hash[in]++;
            while (hash[in] > 1) {
                int out = s.charAt(left);
                hash[out]--;
                left++;
            }
            maxCount = Math.max(maxCount, right - left + 1);
        }
        return maxCount;
    }
}